package com.ruoyi.common.utils.bean;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;

/**
 * @author xumh
 * @title: SpringBeanUtil
 * @projectName ruoyi
 * @description: TODO
 * @date 2024/12/1215:21
 */
@Component
public class SpringBeanUtil extends ApplicationObjectSupport {
    private static ApplicationContext applicationContext = null;

    public SpringBeanUtil() {
    }

    protected void initApplicationContext(ApplicationContext context) throws BeansException {
        super.initApplicationContext(context);
        applicationContext = context;
    }

    public static ApplicationContext getAppContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getAppContext().getBean(name);
    }

    public static <T> T getBeanByType(Class<T> classes) {
        return getAppContext().getBean(classes);
    }

    public static <T> T getBean(Class<T> classes) {
        return getAppContext().getBean(classes);
    }
}
